home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 034a / twview82.zip / PATHSTUF.INC < prev    next >
Text File  |  1991-02-04  |  6KB  |  187 lines

  1. procedure PrintPath( var home : sector; sec : sector );
  2. begin
  3.   if home = sec then
  4.     write( sec )
  5.   else
  6.     begin
  7.       PrintPath( home, distances[ sec ].s );
  8.       write( ' - ', sec );
  9.     end; {induction}
  10. end;
  11.  
  12. function FixPath( start, finish : sector ) : integer;
  13. { Adjusts Distances from start up to point were finish is entered;
  14. returns length of path. }
  15. var
  16.   s : sector;
  17.   breadth : queue;
  18.   daddy, sonny : sector;
  19.   i : warpindex;
  20.   done : boolean;
  21. begin
  22.   for s := 1 to maxSector do
  23.     Distances[s].d := -1;
  24.   breadth.front := 0;
  25.   enqueue( breadth, start, start );
  26.   repeat
  27.       serve( breadth, daddy, sonny );
  28.       if Distances[ sonny ].d = -1 then {haven't hit him before:}
  29.         begin
  30.           distances[ sonny ].d := distances[ daddy ].d + 1;
  31.           distances[ sonny ].s := daddy;
  32.           with space.sectors[ sonny ] do if number > 0 then
  33.             for i := 1 to number do
  34.               enqueue( breadth, sonny, data[ i ] );
  35.           done := sonny = finish;
  36.         end; {if}
  37.   until done or (breadth.front = 0);
  38.   FixPath := distances[ finish ].d;
  39.   for s := 1 to maxSector do
  40.     if distances[s].d = -1 then distances[s].d := maxint;
  41. end; {FixPath}
  42.  
  43. procedure PathLength;
  44. var
  45.   s1, s2 : sector;
  46. begin
  47.   write('Distance between which two sectors? ');
  48.   readln( s1, s2 );
  49.   if space.sectors[ s1 ].number <> Unexplored then
  50.     begin
  51.       if FixPath( s1, s2) = Error then
  52.         writeln('You don''t know how to get to ', s2, ' from ', s1, '!' )
  53.       else
  54.         begin
  55.           writeln('Known shortest path from ', s1, ' to ', s2, ' is ');
  56.           PrintPath( s1, s2 );
  57.           writeln;
  58.         end; {if finite distance}
  59.     end {visited}
  60.   else
  61.     writeln('Never visited ', s1, ' so can''t tell distances leaving it.');
  62.   if space.sectors[ s1 ].number <> UnExplored then
  63.     begin
  64.       if FixPath( s2, s1 ) = Error then
  65.         writeln('You don''t know how to get to ', s1, ' from ', s2, '!' )
  66.       else
  67.         begin
  68.           writeln('Known shortest path from ', s2, ' to ', s1, ' is ');
  69.           PrintPath( s2, s1 );
  70.           writeln;
  71.         end; {if finite distance}
  72.     end
  73.   else
  74.     writeln('Never visited ', s2, ' so can''t tell distances leaving it.');
  75. end;
  76.  
  77. procedure NearestFighters;
  78. var
  79.   s, s1, Closest : sector;
  80. begin
  81.   write('What is your current sector?  ');
  82.   readln( s );
  83.   FixDistances( s, distances );
  84.   Closest := 1;
  85.   for s1 := 1 to maxSector do
  86.     if space.sectors[ s1 ].portType = Class0 then
  87.       if distances[ s1 ].d = maxint then
  88.         writeln('You don''t know how to get to ', s1 )
  89.       else
  90.         begin
  91.           writeln('Path to ', s1, ' is of length ', distances[ s1 ].d );
  92.           PrintPath( s, s1 );
  93.           writeln;
  94.           if distances[ s1 ].d < distances[ Closest ].d then
  95.             Closest := s1;
  96.         end; {for if else}
  97. end; {Nearest Fighters}
  98.  
  99. procedure MajorSpaceLanes;
  100. { The major space lanes consist of the triangle of paths between SpaceDock and
  101. the two class 0 ports (excluding Terra), together with the path between SpaceDock
  102. and Terra. }
  103.  
  104. const
  105.   MaxClass0s = 3;  { If more than 3, code may need to be changed. }
  106. var
  107.   MajorTradeRoute : array [ sector ] of boolean;
  108.   s               : sector;
  109.   Class0s         : array [1..MaxClass0s] of sector;
  110.   i,
  111.   Known0s         : 0..MaxClass0s;
  112.   f               : text;
  113.   count           : integer;
  114.   EchoDisk        : Boolean;
  115.  
  116. procedure TogglePath( home, sec : SectorIndex);
  117. begin
  118.   MajorTradeRoute[ sec ] := true;
  119.   if sec <> home then
  120.     TogglePath( home, distances[ sec ].s );
  121. end; {TogglePath}
  122.  
  123. begin
  124.   if space.dock = 0 then
  125.     writeln('You don''t know where the space dock is?  I''m not going to try...')
  126.   else
  127.     begin
  128.       for s := 1 to MaxSector do
  129.         MajorTradeRoute[ s ] := false;
  130.       Known0s := 0;
  131.       for s := 1 to MaxSector do
  132.         if space.sectors[s].portType = Class0 then
  133.           begin
  134.             Known0s := Known0s + 1;
  135.             Class0s[ Known0s ] := s;
  136.           end; {for if}
  137.        if Known0s <> MaxClass0s then
  138.          writeln('Warning: not all Class 0 ports have been discovered!');
  139.        if Known0s = 0 then
  140.          begin writeln('Incomplete data.  Aborting...'); exit; end;
  141.        write('Class 0 ports: ');
  142.        for i := 1 to Known0s do
  143.          write( Class0s[ i ] : 5 );
  144.        writeln;
  145.        for i := 1 to Known0s do
  146.          begin
  147.            if FixPath( space.dock, Class0s[ i ] ) = Error then
  148.              writeln('You don''t know how to get from the dock to ', Class0s[i], '!')
  149.            else
  150.              TogglePath( space.dock, Class0s[ i ] );
  151.            if FixPath( Class0s[ i ], space.dock ) = Error then
  152.              writeln('You don''t know how to get to the dock from ', Class0s[i], '!')
  153.            else
  154.              TogglePath( Class0s[i], space.dock);
  155.          end; {for}
  156.        if Known0s = 3 then
  157.          begin
  158.            if FixPath( Class0s[2], Class0s[3] ) <> Error then
  159.              TogglePath( Class0s[2], Class0s[3] )
  160.            else
  161.              writeln('You don''t know how to get from ', Class0s[2], ' to ',
  162.                       Class0s[3] );
  163.            if FixPath( Class0s[3], Class0s[3] ) <> Error then
  164.              TogglePath( Class0s[3], Class0s[3] )
  165.            else
  166.              writeln('You don''t know how to get from ', Class0s[3], ' to ',
  167.                       Class0s[2] );
  168.          end; {if}
  169.        EchoDisk := LogToDisk( f, 'Do you want the results echoed to disk? ');
  170.        for s := 1 to maxSector do
  171.          if MajorTradeRoute[ s ] then write( s : 5 );
  172.        if EchoDisk then
  173.          begin
  174.            writeln(f, 'estimated Major Space Lanes');
  175.            count := 0;
  176.            for s := 1 to maxSector do
  177.              if MajorTradeRoute[ s ] then
  178.                begin
  179.                  count := count + 1;
  180.                  write( f, s:5 );
  181.                  if count mod 10 = 0 then writeln( f );
  182.                end; {for if}
  183.            close( f );
  184.          end; {if}
  185.      end; {else}
  186.    writeln;
  187.  end; {SpaceLanes}